package com.hero.tweaks.ui.fragments;

import net.margaritov.preference.colorpicker.ColorPickerPreference;
import android.os.Bundle;
import android.provider.Settings;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

import com.hero.tweaks.R;
import com.hero.tweaks.util.AxisPreferenceFragment;
import com.hero.tweaks.util.Helpers;
import android.preference.CheckBoxPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceScreen;
import android.preference.SwitchPreference;
import android.preference.Preference.OnPreferenceChangeListener;

public class Interface extends AxisPreferenceFragment implements
OnPreferenceChangeListener, Preference.OnPreferenceClickListener {
	
	private static final String PREF_PRISM_LANDSCAPE = "pref_prism_landscape";
	private static final String PREF_ENABLE_NAVBAR = "pref_enable_navbar";
	private static final String PREF_NAVBAR_COLOUR = "pref_navbar_colour";
	//private static final String PREF_MENU_BUTTON = "pref_menu_button";
	//private static final String PREF_SEARCH_BUTTON = "pref_search_button";
	//private static final String PREF_COLOR_PRISM = "pref_prism_color";
	private static final String PREF_ALL_ROTATIONS = "pref_all_rotations";
	private static final String PREF_HOME_ASSIST = "pref_home_assist";
	//private static final String PREF_APPDRAWER_COLOR = "pref_appdrawer_color";
	private static final String PREF_BACK_TO_KILL = "pref_back_to_kill";
	private static final String PREF_VOLUME_ROCKER_CONFIG = "pref_volume_rocker_config";
	private static final String PREF_RECENTS_HIDE_SEARCH = "pref_recents_hide_search";
	//private static final String PREF_HIDE_HOME_TEXT = "pref_hide_home_text";
	private static final String PREF_LARGE_BLINK_PHOTOS = "pref_large_blink_photos";
	
	SwitchPreference mPrismLandscape;
	SwitchPreference mEnableNavBar;
	SwitchPreference mNavBarColour;
	//SwitchPreference mMenuButton;
	//SwitchPreference mSearchButton;
	//Preference mColorPrism;
	SwitchPreference mAllRotations;
	//Preference mPieSettings;
	SwitchPreference mHomeAssist;
	//ColorPickerPreference mAppdrawerColor;
	SwitchPreference mBackToKill;
	ListPreference mVolumeRockerConfig;
	SwitchPreference mRecentsHideSearch;
	//CheckBoxPreference mHideHomeText;
	SwitchPreference mLargeBlinkPhotos;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// Load the preferences from an XML resource
		addPreferencesFromResource(R.xml.prefs_interface);
		
		mPrismLandscape = (SwitchPreference) findPreference(PREF_PRISM_LANDSCAPE);
		mPrismLandscape.setOnPreferenceChangeListener(this);
		mPrismLandscape.setChecked((Settings.System.getInt(
				getContentResolver(), "tweaks_prism_landscape", 0) == 1));
		
		mEnableNavBar = (SwitchPreference) findPreference(PREF_ENABLE_NAVBAR);
		mEnableNavBar.setOnPreferenceChangeListener(this);
		mEnableNavBar.setChecked((Settings.System.getInt(
				getContentResolver(), "tweaks_show_nav_bar", 1) == 1));
		
		mNavBarColour = (SwitchPreference) findPreference(PREF_NAVBAR_COLOUR);
		mNavBarColour.setOnPreferenceChangeListener(this);
		mNavBarColour.setChecked((Settings.System.getInt(
				getContentResolver(), "tweaks_navbar_color", 0) == 1));
		
		/*mMenuButton = (SwitchPreference) findPreference(PREF_MENU_BUTTON);
		mMenuButton.setOnPreferenceChangeListener(this);
		mMenuButton.setChecked((Settings.System.getInt(
				getContentResolver(), "tweaks_show_menu", 0) == 1));*/
		
		/*mSearchButton = (SwitchPreference) findPreference(PREF_SEARCH_BUTTON);
		mSearchButton.setOnPreferenceChangeListener(this);
		mSearchButton.setChecked((Settings.System.getInt(
				getContentResolver(), "tweaks_show_search", 0) == 1));*/
		
		mHomeAssist = (SwitchPreference) findPreference(PREF_HOME_ASSIST);
		mHomeAssist.setOnPreferenceChangeListener(this);
		mHomeAssist.setChecked((Settings.System.getInt(
				getContentResolver(), "tweaks_enable_home_assist", 1) == 1));

		/*mColorPrism = findPreference(PREF_COLOR_PRISM);
		mColorPrism.setOnPreferenceClickListener(this);*/
		
		mAllRotations = (SwitchPreference) findPreference(PREF_ALL_ROTATIONS);
		mAllRotations.setOnPreferenceChangeListener(this);
		mAllRotations.setChecked((Settings.System.getInt(
				getContentResolver(), "tweaks_all_rotations", 0) == 1));
		
		/*mAppdrawerColor = (ColorPickerPreference) findPreference(PREF_APPDRAWER_COLOR);
		mAppdrawerColor.setOnPreferenceChangeListener(this);*/
		
		mBackToKill = (SwitchPreference) findPreference(PREF_BACK_TO_KILL);
		mBackToKill.setOnPreferenceChangeListener(this);
		mBackToKill.setChecked((Settings.System.getInt(
				getContentResolver(), "tweaks_backkey_kill", 0) == 1));
		
		mVolumeRockerConfig = (ListPreference) findPreference(PREF_VOLUME_ROCKER_CONFIG);
		mVolumeRockerConfig.setOnPreferenceChangeListener(this);
		mVolumeRockerConfig.setValue((Settings.System.getInt(getContentResolver(),
				"tweaks_volume_rocker_config", 0)) + "");
		
		mRecentsHideSearch = (SwitchPreference) findPreference(PREF_RECENTS_HIDE_SEARCH);
		mRecentsHideSearch.setOnPreferenceChangeListener(this);
		mRecentsHideSearch.setChecked((Settings.System.getInt(
				getContentResolver(), "tweaks_hide_recents_search", 0) == 1));
		
		/*mHideHomeText = (CheckBoxPreference) findPreference(PREF_HIDE_HOME_TEXT);
		mHideHomeText.setOnPreferenceChangeListener(this);
		mHideHomeText.setChecked((Settings.System.getInt(
				getContentResolver(), "tweaks_hide_homescreen_text", 0) == 1));*/
		
		mLargeBlinkPhotos = (SwitchPreference) findPreference(PREF_LARGE_BLINK_PHOTOS);
		mLargeBlinkPhotos.setOnPreferenceChangeListener(this);
		mLargeBlinkPhotos.setChecked((Settings.System.getInt(
				getContentResolver(), "tweaks_big_blinkfeed", 0) == 1));

		/*mPieSettings = findPreference(PREF_PIE_SETTINGS);
		mPieSettings.setOnPreferenceClickListener(this);*/
				
	}

	@Override
	public boolean onPreferenceChange(Preference preference, Object objValue) {
		Cr = getActivity().getContentResolver();
		
		if (preference == mPrismLandscape) {
			boolean checked = ((SwitchPreference) preference).isChecked();
			int value = (checked ? 0 : 1);
			Settings.System.putInt(getContentResolver(),
					"tweaks_prism_landscape", value);
			setHasOptionsMenu(true);
			return true;
			
		} else if (preference == mEnableNavBar) {
			boolean checked = ((SwitchPreference) preference).isChecked();
			int value = (checked ? 0 : 1);
			Settings.System.putInt(getContentResolver(), "tweaks_show_nav_bar",
					value);
			setHasOptionsMenu(true);
			return true;
			
		} else if (preference == mNavBarColour) {
			boolean checked = ((SwitchPreference) preference).isChecked();
			int value = (checked ? 0 : 1);
			Settings.System.putInt(getContentResolver(), "tweaks_navbar_color",
					value);
			setHasOptionsMenu(true);
			return true;
			
	    /*} else if (preference == mMenuButton) {
			boolean checked = ((SwitchPreference) preference).isChecked();
			int value = (checked ? 0 : 1);
			Settings.System.putInt(getContentResolver(), "tweaks_show_menu",
					value);
			setHasOptionsMenu(true);
			return true;*/
			
	    /*} else if (preference == mSearchButton) {
			boolean checked = ((SwitchPreference) preference).isChecked();
			int value = (checked ? 0 : 1);
			Settings.System.putInt(getContentResolver(), "tweaks_show_search",
					value);
			setHasOptionsMenu(true);
			return true;*/
			
	    } else if (preference == mAllRotations) {
			boolean checked = ((SwitchPreference) preference).isChecked();
			int value = (checked ? 0 : 1);
			Settings.System.putInt(getContentResolver(), "tweaks_all_rotations",
					value);
			setHasOptionsMenu(true);
			return true;

		} else if (preference == mHomeAssist) {
			boolean checked = ((SwitchPreference) preference).isChecked();
			int value = (checked ? 0 : 1);
			Settings.System.putInt(getContentResolver(), "tweaks_enable_home_assist",
					value);
			setHasOptionsMenu(true);
			return true;
			
		} else if (preference == mBackToKill) {
				boolean checked = ((SwitchPreference) preference).isChecked();
				int value = (checked ? 0 : 1);
				Settings.System.putInt(getContentResolver(), "tweaks_backkey_kill",
						value);
				return true;

		} else if (preference == mVolumeRockerConfig) {
					int val = Integer.parseInt((String) objValue);
					Settings.System.putInt(getContentResolver(), "tweaks_volume_rocker_config", val);
					return true;
			
		/*} else if (preference == mAppdrawerColor) {
			String hex = ColorPickerPreference.convertToARGB(Integer
					.valueOf(String.valueOf(objValue)));
			preference.setSummary(hex);

			int intHex = ColorPickerPreference.convertToColorInt(hex);
			Settings.System.putInt(getContentResolver(), "tweaks_appdrawer_background",
					intHex);
			setHasOptionsMenu(true);
			return true;*/
			
		} else if (preference == mRecentsHideSearch) {
			boolean checked = ((SwitchPreference) preference).isChecked();
			int value = (checked ? 0 : 1);
			Settings.System.putInt(getContentResolver(), "tweaks_hide_recents_search",
					value);
			setHasOptionsMenu(true);
			return true;
			
		/*} else if (preference == mHideHomeText) {
			boolean checked = ((CheckBoxPreference) preference).isChecked();
			int value = (checked ? 0 : 1);
			Settings.System.putInt(getContentResolver(), "tweaks_hide_homescreen_text",
					value);
			setHasOptionsMenu(true);
			return true;*/
			
		} else if (preference == mLargeBlinkPhotos) {
			boolean checked = ((SwitchPreference) preference).isChecked();
			int value = (checked ? 0 : 1);
			Settings.System.putInt(getContentResolver(), "tweaks_big_blinkfeed",
					value);
			setHasOptionsMenu(true);
			return true;
			
		}
		
		return false;
	}
	
	@Override
	public boolean onPreferenceClick(Preference preference) {
		/*String key = preference.getKey();
		if (key.equals(PREF_COLOR_PRISM)) {
			Intent myIntent = new Intent(getActivity(), PrismTextColors.class);
			getActivity().startActivity(myIntent);
			return true;
		}*/ 
		return false;
	}

	@Override
	public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen,
			Preference preference) {

		return super.onPreferenceTreeClick(preferenceScreen, preference);
	}
	
	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		super.onCreateOptionsMenu(menu, inflater);
		inflater.inflate(R.layout.actionbar_actions, menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case (R.id.restart_sysui):
			Helpers.restartHotreboot();
			return true;
		default:
			return super.onContextItemSelected(item);
		}
	}
	
}